import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.TitledBorder;
import javax.swing.JList;

import java.awt.Color;

import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.table.TableModel;


public class UpdateStart extends JFrame {

	private JPanel contentPane;
	private ArrayList updates;
	private DefaultTableModel model;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UpdateStart frame = new UpdateStart();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UpdateStart() {
		updates = new ArrayList();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 806, 430);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		String [][] rowdata = {{null,null,null,null}};
		String [] columns = {"Version", "Beschreibung", "Lizenz", "Download Pfad"};
		model = new DefaultTableModel(rowdata,columns);
		
		JButton btnUpdatesSuchen = new JButton("Updates Suchen");
		btnUpdatesSuchen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				URL url;
				try {
					
					url = new URL("http://metromail.24.eu/UpdateVZ.xml");
					Main m = new Main();
					updates=m.checkForUpdates(1000, url, "Hash5", "1");
					System.out.println("Anzahl der Neuen Updates: " + updates.size());
					
					for( int i = 0 ; i < updates.size() ; i++ ){
						
						String[] Update = (String[]) updates.get(i);
						String Version = Update[0];
						String SoftwareName = Update[1];
						String Software_Id = Update[2];
						String Beschreibung = Update[3];
						String Lizenz = Update[4];
						String DownloadPath = Update[5];
						
						System.out.println("Version: " + Version + " Software Name: "  + SoftwareName + " Software ID: " + Software_Id + " Download Path: " + DownloadPath);
						String[] rowData = {Version,Beschreibung,Lizenz,DownloadPath};
						model.addRow(rowData);
						
					}
					
				
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(btnUpdatesSuchen, BorderLayout.NORTH);
		
		JButton btnNewButton = new JButton("Installieren");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Informations i = new Informations();
				i.Download=(String) table.getValueAt(table.getSelectedRow(), 3);
				i.Lizenz=(String) table.getValueAt(table.getSelectedRow(), 2);
				i.Information=(String) table.getValueAt(table.getSelectedRow(), 1);
				i.Version = (String) table.getValueAt(table.getSelectedRow(), 0);
				i.textarea.setText("Update Informationen des Paketes \n Version: " + i.Version + "\n\n--------------------------------------\n" + i.Information);
				i.setVisible(true);
				
			}
		});
		contentPane.add(btnNewButton, BorderLayout.SOUTH);
		
		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable(model);
		scrollPane.setViewportView(table);
		modelRemoveRows();
	}
	
	
	public void modelRemoveRows(){
		
		for( int i = 0 ; i < model.getRowCount() ; i++ ){
			model.removeRow(i);
		}
		
	}
	
}
